package com.jiuyi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuyi.common.R;
import com.jiuyi.entity.ShoppingCart;
import com.jiuyi.entity.User;
import com.jiuyi.exception.SystemException;
import com.jiuyi.mapper.ShoppingCartMapper;
import com.jiuyi.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.jiuyi.emum.HttpMsgMenu.CART_IS_ZERO;

/**
 * DATA: 2022/10/23
 * Author: 晨夕
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    //添加订单
    public R addShoppingCart(ShoppingCart shoppingCart, HttpServletRequest request) {
        //1. 设置user_id，指定当前是哪个用户
        Object userId = request.getSession().getAttribute("user");
        shoppingCart.setUserId(Long.parseLong(userId.toString()));
        //2. 判断是否是菜品还是套餐
        if(!ObjectUtils.isEmpty(shoppingCart.getDishId())){
            //跟着口味查
            ShoppingCart shoppingCartDish = shoppingCartMapper.selectDishByDishFlavor(shoppingCart.getDishFlavor());
            if(ObjectUtils.isEmpty(shoppingCartDish)){
                //2.11 为空则添加，number 默认为1
                shoppingCartMapper.insert(shoppingCart);
            }else {
                //2.12 不为空则修改number + 1
                shoppingCartMapper.updateNumber(shoppingCartDish.getId());
            }
        }else{
            ShoppingCart shoppingCartSetMeal = shoppingCartMapper.selectSetmealById(shoppingCart.getSetmealId());
            if(ObjectUtils.isEmpty(shoppingCartSetMeal)){
                //2.13 为空则添加，number 默认为1
                shoppingCartMapper.insert(shoppingCart);
            }else {
                //2.14 不为空则修改number + 1
                shoppingCartMapper.updateNumber(shoppingCartSetMeal.getId());
            }
        }
        return R.ok(shoppingCart);
    }

    //查看购物车
    @Override
    public R<ShoppingCart> selectShoppingCart(HttpServletRequest request) {
        //根据user_id查购物车
        Object userId = request.getSession().getAttribute("user");
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByUserId(Long.parseLong(userId.toString()));
        return R.ok(shoppingCarts);
    }
    //清空购物车
    @Override
    public R deleteShoppingCart(HttpServletRequest request) {
        Object userId = request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper<>();
        query.eq(ShoppingCart::getUserId,Long.parseLong(userId.toString()));
        shoppingCartMapper.delete(query);
        return R.success("购物车已清空");
    }
    //减少订单
    @Override
    public R subShoppingCart(ShoppingCart shoppingCart, HttpServletRequest request) {
        //1. 设置user_id，指定当前是哪个用户
        Object userId = request.getSession().getAttribute("user");
        shoppingCart.setUserId(Long.parseLong(userId.toString()));
        //2. 判断是否是菜品还是套餐
        if(!ObjectUtils.isEmpty(shoppingCart.getDishId())){
            //跟着口味查
            ShoppingCart shoppingCartDish = shoppingCartMapper.selectDishByDishFlavor(shoppingCart.getDishFlavor());
            //1. 数量-1
            shoppingCartMapper.subNumber(shoppingCartDish.getId());
            //2. 获取数量
            int number = shoppingCartMapper.selectNumber(shoppingCartDish.getId());
            //3. 如果number=0就删除这个订单
            if(number == 0){
                //根据id删除订单
                shoppingCartMapper.deleteById(shoppingCartDish.getId());
                throw new SystemException(CART_IS_ZERO);
            }
        }else{
            ShoppingCart shoppingCartSetMeal = shoppingCartMapper.selectSetmealById(shoppingCart.getSetmealId());
            //1. 数量-1
            shoppingCartMapper.subNumber(shoppingCartSetMeal.getId());
            //2. 获取数量
            int number = shoppingCartMapper.selectNumber(shoppingCartSetMeal.getId());
            //3. 如果number=0就删除这个订单
            if(number == 0){
                //根据id删除订单
                shoppingCartMapper.deleteById(shoppingCartSetMeal.getId());
                throw new SystemException(CART_IS_ZERO);
            }
        }
        return R.ok(shoppingCart);
    }
}
